﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.Web
Imports System.IO
Imports Microsoft.Office.Interop

Partial Class devida_ReporteAvances
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            'llenado de datos

            ViewState("NUM") = 0
            lblTitulo.Text = "Seguimiento de los Planes de Trabajo mensuales"
            ListaPeriodo()
            ddl_PERIODO.SelectedValue = "all"
            If ViewState("Filas") > 0 Then
                CargarGrilla()
            End If
        End If
    End Sub

    Public Sub ListaPeriodo()
        'carga de combo periodo
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPeriodo("PLAN_TRABAJO_INGRESO", Session("Año"))
        ViewState("Filas") = objData.Rows.Count
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Seleccione--"
        drusuariorow("PERIODO") = "all"
        objData.Rows.Add(drusuariorow)
        ddl_PERIODO.DataSource = objData
        ddl_PERIODO.DataTextField = "DESCRIPCION"
        ddl_PERIODO.DataValueField = "PERIODO"
        ddl_PERIODO.DataBind()
    End Sub

    Public Sub CargarGrilla()
        'carga de grilla 
        Dim objReporte As ReporteBC
        Dim objDt As DataTable
        Try
            objReporte = New ReporteBC
            objDt = New DataTable
            ViewState("Persona") = Session("Persona")
            objDt = objReporte.RegistroAvance(ViewState("Persona"), ddl_PERIODO.SelectedValue.ToString)
            gv_impresion.DataSource = objDt
            gv_impresion.DataBind()
            gv_doc.DataSource = objDt
            gv_doc.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub gv_impresion_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_impresion.RowDataBound
        'marcado de checkbox
        Dim hfPlan As HiddenField = CType(e.Row.Cells(2).FindControl("hfPlan"), HiddenField)
        Dim hfAfis As HiddenField = CType(e.Row.Cells(3).FindControl("hfAfis"), HiddenField)
        Dim hfAfin As HiddenField = CType(e.Row.Cells(4).FindControl("hfAfin"), HiddenField)
        If Not (hfPlan Is Nothing) Then
            If hfPlan.Value = "2" Then
                Dim hchk1 As CheckBox = CType(e.Row.Cells(2).FindControl("chk1"), CheckBox)
                If Not (hchk1 Is Nothing) Then
                    hchk1.Checked = True
                    hchk1 = Nothing
                End If
            ElseIf (hfPlan.Value = "1") Or (hfPlan.Value = "&nbsp;") Then
                Dim hchk1 As CheckBox = CType(e.Row.Cells(2).FindControl("chk1"), CheckBox)
                If Not (hchk1 Is Nothing) Then
                    hchk1.Checked = False
                    hchk1 = Nothing
                End If
            End If
        End If
        If Not (hfAfis Is Nothing) Then
            If hfAfis.Value = "2" Then
                Dim hchk2 As CheckBox = CType(e.Row.Cells(3).FindControl("chk2"), CheckBox)
                If Not (hchk2 Is Nothing) Then
                    hchk2.Checked = True
                    hchk2 = Nothing
                End If
            ElseIf (hfAfis.Value = "1") Or (hfAfis.Value = "&nbsp;") Then
                Dim hchk2 As CheckBox = CType(e.Row.Cells(3).FindControl("chk2"), CheckBox)
                If Not (hchk2 Is Nothing) Then
                    hchk2.Checked = False
                    hchk2 = Nothing
                End If
            End If
        End If
        If Not (hfAfin Is Nothing) Then
            If hfAfin.Value = "2" Then
                Dim hchk3 As CheckBox = CType(e.Row.Cells(4).FindControl("chk3"), CheckBox)
                If Not (hchk3 Is Nothing) Then
                    hchk3.Checked = True
                    hchk3 = Nothing
                End If
            ElseIf (hfAfin.Value = "1") Or (hfAfin.Value = "&nbsp;") Then
                Dim hchk3 As CheckBox = CType(e.Row.Cells(4).FindControl("chk3"), CheckBox)
                If Not (hchk3 Is Nothing) Then
                    hchk3.Checked = False
                    hchk3 = Nothing
                End If
            End If
        End If
    End Sub

    Protected Sub ibExportar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibExportar.Click
        ExportarGrilla(gv_doc)
    End Sub

    Private Sub ExportarGrilla(ByVal grilla As GridView)
        'exportacion de reporte a excel
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        ViewState("NUM") = ViewState("NUM") + 1
        'CREAR TITULO
        Dim gvtitle As New Table
        Dim titulo1 As New TableCell()
        titulo1.RowSpan = 1
        titulo1.ColumnSpan = 5
        titulo1.Text = "Lista de Proyectos no Ingresados"
        titulo1.Font.Name = "Calibri"
        titulo1.Font.Size = 12
        titulo1.Font.Bold = True
        titulo1.BackColor = Drawing.Color.White
        titulo1.HorizontalAlign = HorizontalAlign.Center
        titulo1.VerticalAlign = VerticalAlign.Middle
        Dim headertitulo1 As New TableRow()
        headertitulo1.Cells.Add(titulo1)
        gvtitle.Rows.AddAt(0, headertitulo1)
        '------------    
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(gvtitle)
        form.Controls.Add(grilla)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=ReporteAvances_" + ViewState("NUM").ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()
    End Sub

    Protected Sub gv_doc_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_doc.RowDataBound
        'marcado de checkbox
        Dim hfPlan As HiddenField = CType(e.Row.Cells(2).FindControl("hfPlan"), HiddenField)
        Dim hfAfis As HiddenField = CType(e.Row.Cells(3).FindControl("hfAfis"), HiddenField)
        Dim hfAfin As HiddenField = CType(e.Row.Cells(4).FindControl("hfAfin"), HiddenField)
        If Not (hfPlan Is Nothing) Then
            If hfPlan.Value = "2" Then
                Dim hchk1 As Label = CType(e.Row.Cells(2).FindControl("chk1"), Label)
                If Not (hchk1 Is Nothing) Then
                    hchk1.Text = "Si"
                    hchk1 = Nothing
                End If
            ElseIf (hfPlan.Value = "1") Or (hfPlan.Value = "") Then
                Dim hchk1 As Label = CType(e.Row.Cells(2).FindControl("chk1"), Label)
                If Not (hchk1 Is Nothing) Then
                    hchk1.Text = "No"
                    hchk1 = Nothing
                End If
            End If
        End If
        If Not (hfAfis Is Nothing) Then
            If hfAfis.Value = "2" Then
                Dim hchk2 As Label = CType(e.Row.Cells(3).FindControl("chk2"), Label)
                If Not (hchk2 Is Nothing) Then
                    hchk2.Text = "Si"
                    hchk2 = Nothing
                End If
            ElseIf (hfAfis.Value = "1") Or (hfAfis.Value = "") Then
                Dim hchk2 As Label = CType(e.Row.Cells(3).FindControl("chk2"), Label)
                If Not (hchk2 Is Nothing) Then
                    hchk2.Text = "No"
                    hchk2 = Nothing
                End If
            End If
        End If
        If Not (hfAfin Is Nothing) Then
            If hfAfin.Value = "2" Then
                Dim hchk3 As Label = CType(e.Row.Cells(4).FindControl("chk3"), Label)
                If Not (hchk3 Is Nothing) Then
                    hchk3.Text = "Si"
                    hchk3 = Nothing
                End If
            ElseIf (hfAfin.Value = "1") Or (hfAfin.Value = "") Then
                Dim hchk3 As Label = CType(e.Row.Cells(4).FindControl("chk3"), Label)
                If Not (hchk3 Is Nothing) Then
                    hchk3.Text = "No"
                    hchk3 = Nothing
                End If
            End If
        End If
    End Sub

    Protected Sub btn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_salir.Click
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub ddl_PERIODO_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_PERIODO.SelectedIndexChanged
        CargarGrilla()
    End Sub

End Class